package olympicgameslogic;

import java.io.Serializable;
import java.util.Comparator;



/**
 *
 * @author Fábio
 */
public class Delegation implements Serializable, OurComparable, Comparator {

   private Nation country;
   private Edition edition;
   private LinkedList<Athlete> athleteList;
   private LinkedList<Team> teamList;
   private LinkedList<Medal> delegationMedals = new LinkedList();

   public Delegation(Nation country, Edition edition, LinkedList<Athlete> athleteList, LinkedList<Team> teamList) {
      
      
      setCountry(country);
      setEdition(edition);
      setAthleteList(athleteList);
      setTeamList(teamList);
   }

   public Delegation(Nation country, Edition edition) {
      this(country, edition, new LinkedList<Athlete>(), new LinkedList<Team>());
   }

   /**
    * @return the country
    */
   public Nation getCountry() {
      return country;
   }

   /**
    * @return the edition
    */
   public Edition getEdition() {
      return edition;
   }

   /**
    * @return the athleteList
    */
   public LinkedList<Athlete> getAthleteList() {
      return athleteList;
   }

   /**
    * @return the teamList
    */
   public LinkedList<Team> getTeamList() {
      return teamList;
   }

   /**
    * @param country the country to set
    */
   public void setCountry(Nation country) {
      this.country = country;
   }

   /**
    * @param edition the edition to set
    */
   public void setEdition(Edition edition) {
      this.edition = edition;
   }

   /**
    * @param athleteList the athleteList to set
    */
   public void setAthleteList(LinkedList<Athlete> athleteList) {
      this.athleteList = athleteList;
   }

   /**
    * @param teamList the teamList to set
    */
   public void setTeamList(LinkedList<Team> teamList) {
      this.teamList = teamList;
   }

   public void addAthlete(Athlete a) {
      athleteList.add(a);
   }

   public void addTeam(Team t) {
      teamList.add(t);

   }

   public LinkedList<Medal> getDelegationMedals() {
      return delegationMedals;
   }

   @Override
   public String toString() {
      return super.toString();
   }
   
   
   public LinkedList<Medal> getModalityMedals(Modality m){
      LinkedList<Medal> medals = new LinkedList<>();
      for (Medal med : delegationMedals) {
	 if(med.getModality().equals(m))
	    medals.add(med);
      }
      return medals;
   }
   
    public LinkedList<Medal> getDisciplineMedals(Discipline d){
      LinkedList<Medal> medals = new LinkedList<>();
      for (Medal med : delegationMedals) {
	 if(med.getDiscipline().equals(d))
	    medals.add(med);
      }
      return medals;
   }

   public Athlete getAthlete(String athleteName) {
      for (int i = 0; i < athleteList.getSize(); i++) {
	 Athlete a = (Athlete) athleteList.getElement(i);
	 if (a.getName().equalsIgnoreCase(athleteName)) {
	    return a;
	 }

      }
      return null;
   }

 
   public void addMedal(Medal m) {
     delegationMedals.add(m);
   }

   @Override
   public boolean isEqual(Object obj) {
      throw new UnsupportedOperationException("Not supported yet.");
   }

   @Override
   public int compare(Object o1, Object o2) {
      throw new UnsupportedOperationException("Not supported yet.");
   }

 
 
 
   


     public static class MedalsWeightedComparator implements Comparator {

      public int compare(Object o1, Object o2) {
	 if (!(o1 instanceof Delegation) || !(o2 instanceof Delegation)) {
	    throw new ClassCastException();
	 }


	 Delegation d1 = (Delegation) o1;
	 Delegation d2 = (Delegation) o2;

	 return (int) (d2.getMedalsWeighted() - d1.getMedalsWeighted());
      }
   }
     
      public int getMedalsWeighted() {
      int totalWeight = 0;
      //this.bronzeMedals = this.silverMedals = this.goldMedals = 0;
      for (Medal medal : delegationMedals) {
	 if (medal.getMedalType().equals(Medal.MEDALTYPE.GOLD)) {
	    totalWeight = totalWeight + 3;


	 } else if (medal.getMedalType().equals(Medal.MEDALTYPE.SILVER)) {
	    totalWeight = totalWeight + 2;

	 } else if ((medal.getMedalType().equals(Medal.MEDALTYPE.BRONZE))) {
	    totalWeight = totalWeight + 1;
	 }

      }
      return totalWeight;

   }
   
   
   public static class MedalsAbsoluteComparator implements Comparator {
 
      @Override
    public int compare(Object o1, Object o2) {
      if (!(o1 instanceof Delegation) || !(o2 instanceof Delegation))
        throw new ClassCastException();

      
      Delegation d1 = (Delegation) o1;
      Delegation d2 = (Delegation) o2;

      return (int) (d2.getDelegationMedals().size() - d1.getDelegationMedals().size());
    }
  }

 public static class DelegationName implements Comparator {
 
      @Override
    public int compare(Object o1, Object o2) {
      if (!(o1 instanceof Delegation) || !(o2 instanceof Delegation))
        throw new ClassCastException();

      
      Delegation d1 = (Delegation) o1;
      Delegation d2 = (Delegation) o2;

      return (int) (d1.getCountry().getName().compareTo(d2.getCountry().getName()));
    }
  }
   
 
  public int getMedals(Medal.MEDALTYPE medalType) {
      int counter = 0;
      for (Medal m : delegationMedals) {
	 if (m.getMedalType().equals(medalType)) {
	    counter++;
	 }
      }
      return counter;
   }
  
  
  public int getTotalMedals(){
     return delegationMedals.size();
  }
}